package com.gxa.shop.servlet.address;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.shop.bean.dto.AddressQDto;
import com.gxa.shop.bean.dto.Login;
import com.gxa.shop.bean.vo.AddressQ;
import com.gxa.shop.bean.vo.AddressVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.address.AddressService;
import com.gxa.shop.service.address.impl.AddressServiceImpl;
import com.gxa.shop.util.MyFormatUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "QSelectServlet",value = "/select/addr")
public class QSelectServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        BufferedReader stringBuffer=req.getReader();
//        String data=stringBuffer.lines().collect(Collectors.joining());
//        // 将数据转换成json对象
//        JSONObject jsonObject = JSON.parseObject(data);
//        Integer id= jsonObject.getInteger("userId");
//        String name= jsonObject.getString("recipientName");
//        String phone=jsonObject.getString("recipientPhone");
//        Integer regionId= jsonObject.getInteger("recipientRegion");
//        String addr= jsonObject.getString("recipientAddress");
//        BufferedReader bufferedReader=req.getReader();
//        // 获得数据
//        String data=bufferedReader.lines().collect(Collectors.joining());
//        // 将数据转换成json对象
//        JSONObject jsonObject = JSON.parseObject(data);
//        Integer id= jsonObject.getInteger("userId");
        Integer id= MyFormatUtils.toInteger(req.getParameter("userId"));

        AddressService addressService=new AddressServiceImpl();
        List<AddressQ> list=addressService.selectAddr(id);
        R r;
        r=list!=null?R.ok().addData("list",list):R.error("查询失败");

        String listResult= JSON.toJSONString(r);
        System.out.println(listResult);
        PrintWriter writer=resp.getWriter();
        writer.write(listResult);
        writer.flush();
        writer.close();


    }
}
